import type { Product } from '@/types/product'
import { defineStore } from 'pinia'
import { ref } from 'vue'

// 定义"我的"页面下二级路由的数据的 store
export const useMineProductStore = defineStore('mineProduct', () => {
  // 存储"已发布"商品列表
  const publishedList = ref<Product[]>([])
  // 标记"已发布"商品列表是否还有更多商品
  const hasMorePublished = ref(true)
  const publishedCurrentPage = ref<number>(1)

  // 存储"已卖出"商品列表
  const soldList = ref<Product[]>([])
  // 标记"已卖出"商品列表是否还有更多商品
  const hasMoreSold = ref(true)
  const soldCurrentPage = ref<number>(1)

  // 存储"已买到"商品列表
  const purchasedList = ref<Product[]>([])
  // 标记"已买到"商品列表是否还有更多商品
  const hasMorePurchased = ref(true)
  const purchasedCurrentPage = ref<number>(1)

  // 存储"已收藏"商品列表
  const collectedList = ref<Product[]>([])
  // 标记"已收藏"商品列表是否还有更多商品
  const hasMoreCollected = ref(true)
  const collectedCurrentPage = ref<number>(1)

  // 每页显示的商品数量
  const pageSize = ref<number>(10)

  // 重置所有分页数据
  const resetAll = () => {
    publishedCurrentPage.value = 1
    soldCurrentPage.value = 1
    purchasedCurrentPage.value = 1
    collectedCurrentPage.value = 1
    publishedList.value = []
    soldList.value = []
    purchasedList.value = []
    collectedList.value = []
    hasMorePublished.value = true
    hasMoreSold.value = true
    hasMorePurchased.value = true
    hasMoreCollected.value = true
  }

  // 重置发布列表
  const resetPublished = () => {
    publishedCurrentPage.value = 1
    publishedList.value = []
    hasMorePublished.value = true
  }

  // 重置卖出列表
  const resetSold = () => {
    soldCurrentPage.value = 1
    soldList.value = []
    hasMoreSold.value = true
  }

  // 重置买到列表
  const resetPurchased = () => {
    purchasedCurrentPage.value = 1
    purchasedList.value = []
    hasMorePurchased.value = true
  }

  // 重置收藏列表
  const resetCollected = () => {
    collectedCurrentPage.value = 1
    collectedList.value = []
    hasMoreCollected.value = true
  }

  return {
    publishedList,
    soldList,
    purchasedList,
    collectedList,
    hasMorePublished,
    hasMoreSold,
    hasMorePurchased,
    hasMoreCollected,
    publishedCurrentPage,
    soldCurrentPage,
    purchasedCurrentPage,
    collectedCurrentPage,
    pageSize,
    resetAll,
    resetPublished,
    resetSold,
    resetPurchased,
    resetCollected,
  }
})
